<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="数据源类型" prop="dsType" label-width="90px">
        <el-select v-model="queryParams.dsType" placeholder="请选择数据源类型" clearable size="small" @change="handleQuery()">
          <el-option
            v-for="dict in dict.type.zd_ds_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="连接名称" prop="dsName">
        <el-input
          v-model="queryParams.dsName"
          placeholder="请输入连接名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['dmpdatacollect:datasource:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['dmpdatacollect:datasource:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['dmpdatacollect:datasource:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['dmpdatacollect:datasource:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="datasourceList" @row-dblclick="showForm" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="主键" width="55" align="center" prop="dsId"/>
      <el-table-column label="数据源类型" align="center" prop="dsType"/>
      <el-table-column label="连接名称" align="center" prop="dsName"/>
      <el-table-column label="描述" align="center" prop="dsDesc"/>
      <el-table-column label="驱动名称" align="center" prop="dsDriver"/>
      <el-table-column label="连接地址" align="center" prop="dsUrl"/>
      <el-table-column label="连接用户名" align="center" prop="dsUsername"/>
      <el-table-column label="操作" align="center" fixed="right" width="140" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="small"
            type="text"
            icon="el-icon-reading"
            @click="showForm(scope.row)"
          >查看
          </el-button>
          <el-button
            size="small"
            type="text"
            icon="el-icon-money"
            @click="copyRow(scope.row)"
            v-hasPermi="['dmpdatacollect:datasource:copy']"
          >复制
          </el-button>
          <br>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['dmpdatacollect:datasource:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['dmpdatacollect:datasource:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改数据采集源对话框 -->
    <el-dialog :title="title" :visible.sync="open" :close-on-click-modal="false" width="700px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="数据源类型" prop="dsType">
          <el-select v-model="form.dsType" placeholder="请选择数据源类型">
            <el-option
              v-for="dict in dict.type.zd_ds_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="连接名称" prop="dsName">
          <el-input v-model="form.dsName" placeholder="请输入连接名称"/>
        </el-form-item>
        <el-form-item label="描述" prop="dsDesc">
          <el-input v-model="form.dsDesc" placeholder="请输入描述"/>
        </el-form-item>
        <el-form-item label="驱动名称" prop="dsDriver">
          <el-input v-model="form.dsDriver" placeholder="请输入驱动名称"/>
        </el-form-item>
        <el-form-item label="连接地址" prop="dsUrl">
          <el-input v-model="form.dsUrl" placeholder="请输入连接地址"/>
        </el-form-item>
        <el-form-item label="连接用户名" prop="dsUsername">
          <el-input v-model="form.dsUsername" placeholder="请输入连接用户名"/>
        </el-form-item>
        <el-form-item label="连接密码" prop="dsPassword">
          <el-input v-model="form.dsPassword" placeholder="请输入连接密码" type="password" auto-complete="new-password"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button v-if="isShowConfimBtn" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {
    listDatasource,
    getDatasource,
    delDatasource,
    addDatasource,
    updateDatasource,
    copyDatasource
  } from "@/api/dmpdatacollect/datasource";

  export default {
    name: "Datasource",
    dicts: ['zd_ds_type'],
    data() {
      return {
        // 是否显示确定/提交按钮,默认true显示
        isShowConfimBtn: true,
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 数据采集源表格数据
        datasourceList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          dsType: null,
          dsName: null,
          dsDesc: null,
          dsDriver: null,
          dsUrl: null,
          dsUsername: null,
          dsPassword: null,
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          dsType: [
            {required: true, message: "数据源类型不能为空", trigger: "change"}
          ],
          dsName: [
            {required: true, message: "连接名称不能为空", trigger: "blur"}
          ],
          dsDriver: [
            {required: true, message: "驱动名称不能为空", trigger: "blur"}
          ],
          dsUrl: [
            {required: true, message: "连接地址不能为空", trigger: "blur"}
          ],
          createTime: [
            {required: true, message: "创建日期不能为空", trigger: "blur"}
          ],
          updateTime: [
            {required: true, message: "更新时间不能为空", trigger: "blur"}
          ]
        }
      };
    },
    created() {
      this.getList();
    },
    methods: {
      /** 查询数据采集源列表 */
      getList() {
        this.loading = true;
        listDatasource(this.queryParams).then(response => {
          this.datasourceList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          dsId: null,
          dsType: null,
          dsName: null,
          dsDesc: null,
          dsDriver: null,
          dsUrl: null,
          dsUsername: null,
          dsPassword: null,
          createBy: null,
          createTime: null,
          updateBy: null,
          updateTime: null
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.dsId)
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加数据采集源";
        this.isShowConfimBtn = true;// 显示确定按钮
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const dsId = row.dsId || this.ids
        getDatasource(dsId).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改数据采集源";
          this.isShowConfimBtn = true;// 显示确定按钮
        });
      },
      /** 查看操作 */
      showForm(row) {
        this.reset();
        const dsId = row.dsId || this.ids
        getDatasource(dsId).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "查看数据采集源";
          this.isShowConfimBtn = false;// 隐藏确定按钮
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.dsId != null) {
              updateDatasource(this.form).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addDatasource(this.form).then(response => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const dsIds = row.dsId || this.ids;
        this.$modal.confirm('是否确认删除数据采集源编号为"' + dsIds + '"的数据项？').then(function () {
          return delDatasource(dsIds);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {
        });
      },
      /** 导出按钮操作 */
      handleExport() {
        this.download('dmpdatacollect/datasource/export', {
          ...this.queryParams
        }, `datasource_${new Date().getTime()}.xlsx`)
      },
      /** 复制按钮操作 */
      copyRow(row) {
        const dsId = row.dsId;
        const dsName = row.dsName;
        this.$modal.confirm('是否复制名为 ' + dsName + " 的连接？").then(function () {
          return copyDatasource(dsId);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("复制成功");
        }).catch(() => {
        });
      },

    }
  };
</script>
